package com.yndsoft.jc.ui.categorypage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ProductAttribute;
import com.yndsoft.jc.widget.StarBar;

import java.util.ArrayList;
import java.util.List;

public class ProductDetailAdapter extends BaseAdapter {

    List<ProductAttribute> data = new ArrayList<>();
    Context mContext;
    LayoutInflater inflater;

    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;

    public ProductDetailAdapter(Context mContext) {
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<ProductAttribute> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    /**
     * 单选接口
     *
     * @param checkInterface
     */
    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    /**
     * 改变商品数量接口
     *
     * @param modifyCountInterface
     */
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ShopViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ShopViewHolder();
            convertView = inflater.inflate(R.layout.item_product_detail, null);
            viewHolder.detail_check_item1 = convertView.findViewById(R.id.detail_check_item1);
            viewHolder.detail_check_item2 = convertView.findViewById(R.id.detail_check_item2);
            viewHolder.detail_check_item3 = convertView.findViewById(R.id.detail_check_item3);
            viewHolder.detail_describe = convertView.findViewById(R.id.detail_describe);
//            viewHolder.detail_cart_num = convertView.findViewById(R.id.detail_cart_num);
            viewHolder.detail_limit = convertView.findViewById(R.id.detail_limit);
            viewHolder.detail_check_time = convertView.findViewById(R.id.detail_check_time);
            viewHolder.detail_corner_image = convertView.findViewById(R.id.detail_corner_image);
            viewHolder.detail_item_shoppingcart = convertView.findViewById(R.id.detail_item_shoppingcart);
            viewHolder.detail_item_price = convertView.findViewById(R.id.detail_item_price);
            viewHolder.productdet_starBar = convertView.findViewById(R.id.productdet_starBar);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ShopViewHolder) convertView.getTag();
        }

        viewHolder.detail_check_item1.setText(data.get(position).getProject());
        viewHolder.detail_check_item2.setText(data.get(position).getMethod());
        viewHolder.detail_check_item3.setText(data.get(position).getStandard());
        viewHolder.detail_describe.setText(data.get(position).getShortnote());
        viewHolder.detail_item_price.setText("￥" + data.get(position).getPrice());
//        viewHolder.detail_cart_num.setText("*" + data.get(position).getNum());
        viewHolder.detail_limit.setText("最低送样量：" + data.get(position).getLimit() + "g");
        viewHolder.detail_check_time.setText("检测周期：" + data.get(position).getTime() + "个工作日");
        viewHolder.productdet_starBar.setStarMark(data.get(position).getLevel());

        viewHolder.productdet_starBar.setClickAble(false);

        final ProductAttribute itemBean = data.get(position);
        final boolean isChoose = itemBean.isChoose();
        if (isChoose) {
            viewHolder.detail_item_shoppingcart.setBackground(mContext.getResources().getDrawable(R.drawable.shape_shopitem_stoke));
            viewHolder.detail_corner_image.setVisibility(View.VISIBLE);
        } else {
            viewHolder.detail_item_shoppingcart.setBackground(mContext.getResources().getDrawable(R.drawable.shape_shopitem_stoke_white));
            viewHolder.detail_corner_image.setVisibility(View.GONE);
        }
        final ShopViewHolder finalViewHolder = viewHolder;
        viewHolder.detail_item_shoppingcart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isChoose) {
                    finalViewHolder.detail_item_shoppingcart.setBackground(mContext.getResources().getDrawable(R.drawable.shape_shopitem_stoke));
                    finalViewHolder.detail_corner_image.setVisibility(View.VISIBLE);
                } else {
                    finalViewHolder.detail_item_shoppingcart.setBackground(mContext.getResources().getDrawable(R.drawable.shape_shopitem_stoke_white));
                    finalViewHolder.detail_corner_image.setVisibility(View.GONE);
                }
                itemBean.setChoose(!isChoose);
                checkInterface.checkGroup(position, !isChoose); // 向外暴露接口
            }
        });
        return convertView;
    }

    class ShopViewHolder {
        TextView detail_check_item1, detail_check_item2, detail_check_item3, detail_item_price, detail_cart_num, detail_describe, detail_limit, detail_check_time;
        RelativeLayout detail_item_shoppingcart;
        ImageView detail_corner_image;
        StarBar productdet_starBar;
    }

    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param position  元素位置
         * @param isChecked 元素选中与否
         */
        void checkGroup(int position, boolean isChecked);
    }


    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int position, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int position, View showCountView, boolean isChecked);

        /**
         * 删除子item
         *
         * @param position
         */
        void childDelete(int position);
    }
}
